<?php
namespace app\common\model;
use think\Model;
use think\Session;
use think\Validate;
/**
* 人员关系映射
* @author darker <416319808@qq.com>
*/
class Relation extends Model
{

	/**
     * 申请列表
     * @param  array   $map       [条件]
     * @param  array   $pageParam [搜索分页]
     * @param  integer $pageSize  [每页条数]
     * @return [type]             [description]
     */
    public function getList($map = [], $pageParam = [], $pageSize = 15)
    {
    	$map['relation.rid'] = ['not in', [49, 50, 51]];	// 排除资源人身份

        $result = self::order('ctime','desc');
		$result = $result->where($map)
						->join('user u', 'u.uid = relation.uid and relation.auth = 0')
						->field('u.uid,u.name,relation.ctime')
						->group('relation.uid')
						->paginate($pageSize, false, $pageParam);
        return $result;
    }


	/**
	 * 获取用户普通身份
	 * @param  integer  $id   用户uid
	 * @param  boolean  $type false数组，true字符串
	 * @param  integer  $pattern [0未认证|1通过|2未通过]
	 * @param  boolean  $pattern 为true时获取包括资源人的身份
	 * @return array
	 */
	public function identity($uid, $type = false, $pattern = null, $hidden = false)
	{
		$map = [
			'a.uid' => $uid,
			'a.rid' => ['neq', 0],
			'is_del' => 0
		];

		if (!$hidden) {
			$map['a.rid'] = ['not in', [50, 51]];
		}

		if (!is_null($pattern)) {
			$map['a.auth'] = ['eq', $pattern];
		}


		$data = $this->alias('a')
			->where($map)
			->join('role b', 'a.rid = b.id')
			->join('user c', 'c.uid = a.auditor','left')
			->field('a.auth,opinion,ctime,auditime,rid,b.name identity,c.name auditor')
			->select();

		if ($type) {
			$str = '';
			foreach ($data as $index => $item) {
				$str .= $item->data['identity'] . '|';
			}
			return rtrim($str, '|');
		}
		return endoceArr($data);
	}


	/**
	 * 查询已审核用户(除资源人)
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function getRid($uid)
	{
		$data = $this->identity($uid, false, 1);
		return $ids = array_map(create_function('$item', 'return $item[\'rid\'] ;'), $data);
	}

	/**
	 * [获取用户所有已审核身份]
	 * @return [array] [description]
	 */
	public function getAlltype($uid, $type = false, $pattern = null, $hidden = false){

		$data = $this->identity($uid, $type,$pattern,$hidden);
		$res = array();
		if($data){
			foreach ($data as $value) {
				$res[]=$value['rid'];
			}
		}
		return $res;
	}





	/**
	 * 获取资源人
	 * @param  integer $uid 推荐人uid
	 * @param  integer $auth [0未认证|1通过|2未通过]
	 * @return array
	 */
	public function get_resource_man($uid, $auth = null)
	{
		$map['r.rid'] = ['in', [50, 51]];
		$map['r.referral'] = $uid ? $uid : session('user.id');	// 推荐人uid
		$map['u.ban'] = 0;

		if (!is_null($auth)) {
			$map['r.auth'] = ['eq', $auth];
		}

		$data = $this->alias('r')
					->where($map)
					->join('user u', 'r.uid = u.uid')
					->field('r.uid,r.rid,r.auth,r.tel,r.ctime,u.name,u.phone,u.photo')
					->select();

		foreach ($data as $index => $item) {
			if ($item['photo']) {
				$data[$index]['photo'] = $item['photo'] ? '/uploads/' . $item['photo'] : '';
			}
		}

		if ($data) {
			return endoceArr($data);
		} else {
			return [];
		}
		
	}


    /**
     * 获取某字段信息
     */
    public function getOne($where,$field="*"){
        $result = self::field($field)->where($where)->find();
        if($result){
            $result = $result->getData();
        }
        return $result;
    }


    /**
     * 获取推荐人
     * @param  integer $uid 
     * @return array
     */
    public function recommender($uid)
    {
    	return $this->alias('r')
		    		->where(['r.uid' => $uid])
					->join('user u', 'u.uid = r.referral')
					->field('u.uid,name,phone')
					->find()
					->getData();
    }


    /**
     * 身份审核
     * @param   $data 	审核数据
     * @return 	boolean
     */
    public function audit($data)
    {
    	$map = [
    		'uid' => $data['uid'],
    		'rid' => $data['rid']
    	];
    	unset($data['uid']);
    	unset($data['rid']);

    	if ($res = $this->where($map)->update($data)) {

	    	$content = $data['auth'] == 1 ? '您的申请已通过' : '您的申请被拒绝';
			return $this->msg($data['uid'], $content, 0);
    	}
		return $res;    		
    }


    /**
     * 消息提示
     * @param  integer $uid     用户id
     * @param  string $content  消息内容
     * @param  integer $type    消息类型
     * @return boolean
     */
    public function msg($uid, $content, $type)
    {
    	$model = model('Message');
    	return $model->sendMessage(session('user.id'), $uid, $content, 0, '');
    }


    /**
     * 审核资源人
     * @param  integer $auth  审核结果
     * @param  integer $uid   申请人id
     * @param  integer $rid   角色id
     * @param  integer $tel   推荐人手机
     * @param  integer $ctime 申请创建时间
     * @return boolean
     */
	public function audit_resource_man($auth, $uid, $rid, $tel, $ctime)
	{
		if ($auth != 1 && $auth != 2) {
			return false;
		}
		
		$handle = self::where([
				'uid' 		=> $uid,
				'rid' 		=> $rid,
				'tel' 		=> $tel,
				'ctime' 	=> $ctime,
				'referral' 	=> session('user.id'),
			])->update([
				'auth' => $auth,
				'auditime' => time()
			]);
		
		if ($handle) {
			$content = $auth == 1 ? '您的申请已通过' : '您的申请被拒绝';
			return $this->msg($uid, $content, 0);
		}

		return false;
	}



}